package com.uchengwang.ui.cm_ui.me.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.ClubConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.functions.http.manager.OSSManager;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.umsg.bean.ClubInfoBean;
import com.uchengwang.ui.cm_ui.umsg.bean.SchoolClubBean;
import com.uchengwang.ui.utils.DateUtil;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.io.File;
import java.util.List;

import me.shaohui.advancedluban.OnCompressListener;

/**
 * Created by U城网 on 2017/6/12.
 * 社团个人信息
 */

public class MyClubInfoActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private LinearLayout clubInfo_desc_rl, name_rl, sign_rl;
    private TextView name, school, buildTime, sign, desc;
    private ImageView head;
    private String clubLogo, headUrl;//上传成功的头像 服务器获取的头像
    private SchoolClubBean schoolClubBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_club_info);
        initView();

    }

    private void initView(){
        head = (ImageView) findViewById(R.id.clubInfo_head);
        head.setOnClickListener(this);

        name_rl = (LinearLayout) findViewById(R.id.clubInfo_name_rl);
        name_rl.setOnClickListener(this);
        name = (TextView) findViewById(R.id.clubInfo_name);

        clubInfo_desc_rl = (LinearLayout) findViewById(R.id.clubInfo_desc_rl);
        clubInfo_desc_rl.setOnClickListener(this);


        sign_rl = (LinearLayout) findViewById(R.id.clubInfo_sign_rl);
        sign_rl.setOnClickListener(this);
        sign = (TextView) findViewById(R.id.clubInfo_sign);
        school = (TextView) findViewById(R.id.clubInfo_school);
        buildTime = (TextView) findViewById(R.id.clubInfo_time);


        desc = (TextView) findViewById(R.id.clubInfo_desc);

        getClubInfo();


    }

    /**
     * 获取社团信息
     * */

    private void getClubInfo(){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.ID, UCommonUtil.getUtil().getId());
        HttpManager.newInstance(this).query(ClubConfig.Club.method, ClubConfig.Club.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                UCommonUtil.getUtil().showToast(MyClubInfoActivity.this, "获取信息失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                if (status != 0){
                    UCommonUtil.getUtil().showToast(MyClubInfoActivity.this, msg);
                    return;
                }

                if (!TextUtils.isEmpty(data)){
                    ClubInfoBean bean = JsonUtils.getManager().stringToBean(data, ClubInfoBean.class);
                    List<SchoolClubBean> list = bean.getLinkOrg();
                    if (list != null && list.size() >0){
                        schoolClubBean = bean.getLinkOrg().get(0);//校吧
                    }
                    if (bean != null){
                        headUrl = bean.getLogoImg();
                        if (TextUtils.isEmpty(headUrl)){
                            ImageManager.getInstance().loadImage(MyClubInfoActivity.this, head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                        }else {
                            ImageManager.getInstance().loadImage(MyClubInfoActivity.this, head, headUrl, ImageManager.ImageType.CIRCLE);
                        }

                        name.setText(bean.getOrgName());
                        school.setText(bean.getUniName());
                        buildTime.setText(DateUtil.getDateUtil().getDateByMillis(DateUtil.DATE_FORMAT_YMD, bean.getBuildTime()));

                        String signS = bean.getOrgSign();
                        if (TextUtils.isEmpty(signS)){
                            sign.setHint("未设置");
                        }else {
                            sign.setText(bean.getOrgSign());
                        }


                        String description = bean.getOrgDescription();
                        if (TextUtils.isEmpty(description)){
                            desc.setHint("未设置");
                        }else {
                            desc.setText(description);
                        }

                    }

                }


            }
        });


    }


    @Override
    public void onClick(View view) {
        Intent intent = null;

        switch (view.getId()){
            case R.id.clubInfo_head:
                chooseImage();
                break;

            case R.id.clubInfo_sign_rl:
                intent = new Intent(this, EditorInfoActivity.class);
                intent.putExtra(BaseConfig.TYPE_STRING, sign.getText().toString());
                intent.putExtra(BaseConfig.TYPE, 6);
                break;

            case R.id.clubInfo_desc_rl:
                intent = new Intent(this, EditorInfoActivity.class);
                intent.putExtra(BaseConfig.TYPE_STRING, desc.getText().toString());
                intent.putExtra(BaseConfig.TYPE, 9);
                break;
        }

        if (intent != null){
            startActivityForResult(intent, 0);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (BaseConfig.isRelease){
//            getMyInfo();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        BaseConfig.isRelease = false;
    }


    /**
     * 选择图片
     * */
    private void chooseImage(){
        UCommonUtil.getUtil().openAndCrop(this, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                Log.d("jjf", "裁剪的地址="+data);
                String headUrl = data;
                if (!TextUtils.isEmpty(headUrl)){
                    ImageManager.getInstance().loadImage(MyClubInfoActivity.this, head, headUrl, ImageManager.ImageType.CIRCLE);
                    compress(headUrl);
                }
            }
        });

    }


    /**
     * 压缩图片在上传
     * */
    private void compress(String path){
        UCommonUtil.getUtil().compressImageAlone(this, 300, path, new OnCompressListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(File file) {
                String heads = file.getAbsolutePath();//压缩后的想地址

                //上传到阿里云
                OSSManager.getOSSManager().putHeadToOss(heads, new HttpCallBack() {
                    @Override
                    public void onError(int status, String msg) {
                        UCommonUtil.getUtil().showToast(MyClubInfoActivity.this, "图片上传失败");
                        if (TextUtils.isEmpty(headUrl)){
                            ImageManager.getInstance().loadImage(MyClubInfoActivity.this, head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                        }else {
                            ImageManager.getInstance().loadImage(MyClubInfoActivity.this, head, headUrl, ImageManager.ImageType.CIRCLE);
                        }
                    }

                    @Override
                    public void onResp(int status, String msg, String data) {
                        //上传成功返回的oss的头像地址
                        clubLogo = data;
                        MapRequest map = new MapRequest();
                        map.put(BaseConfig.LOGO_IMG, data);
                        putInfo(map);
                    }
                });

            }

            @Override
            public void onError(Throwable e) {
                UCommonUtil.getUtil().showToast(MyClubInfoActivity.this, "图片选择错误");
            }
        });
    }

    /**
     * 将信息提交到我们自己的服务器
     * */
    private void putInfo(MapRequest map){
        HttpManager.newInstance(this).query(ClubConfig.Club.method_change, ClubConfig.Club.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                UCommonUtil.getUtil().showToast(MyClubInfoActivity.this, "修改失败");
                if (TextUtils.isEmpty(headUrl)){
                    ImageManager.getInstance().loadImage(MyClubInfoActivity.this, head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                }else {
                    ImageManager.getInstance().loadImage(MyClubInfoActivity.this, head, headUrl, ImageManager.ImageType.CIRCLE);
                }
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status ==0 ){
                    UCommonUtil.getUtil().showToast(MyClubInfoActivity.this, "修改成功");
                    UChengApplication.userAvatar = clubLogo;
                    return;
                }
                UCommonUtil.getUtil().showToast(MyClubInfoActivity.this, msg);
                if (TextUtils.isEmpty(headUrl)){
                    ImageManager.getInstance().loadImage(MyClubInfoActivity.this, head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                }else {
                    ImageManager.getInstance().loadImage(MyClubInfoActivity.this, head, headUrl, ImageManager.ImageType.CIRCLE);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode ==1 && requestCode ==0 && data != null){
            String content = data.getStringExtra(BaseConfig.CONTENT);
            int type = data.getIntExtra(BaseConfig.TYPE, -1);

            switch (type){
                case 6://修改社团标语
                    sign.setText(content);
                    break;
                case 7://合作联系人
                    break;
                case 8://联系电话
                    break;
                case 9://社团简介
                    desc.setText(content);
                    break;

            }


        }
    }
}
